package com.zhongwang.business.module.baseCostManage.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.dao.InputImportCostDao;
import com.zhongwang.business.module.baseCostManage.model.DictCommonModel;
import com.zhongwang.business.module.baseCostManage.model.InputExtProPlanModel;
import com.zhongwang.business.module.baseCostManage.model.InputRolledSteelPlanModel;
import com.zhongwang.business.util.ShiroUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  外协计划数据导入 listener
 * @author kxx
 * @date 2020年10月26日18:45
 */
public class InputExtProPlanListener extends AnalysisEventListener<InputExtProPlanModel> {


    private InputImportCostDao inputImportCostDao;
    private ImportCallBack callBack;
    private List<InputExtProPlanModel> inputExtProPlanModel = new ArrayList<>();
    private List<DictCommonModel> dictCommon;
    private List<Integer> exceptionList = new ArrayList<>();
    private int i = 2;
    private InputExtProPlanModel extProPlan;

    public InputExtProPlanListener(InputExtProPlanModel extProPlan, InputImportCostDao inputImportCostDao,
                                   ImportCallBack callBack,
                                   List<DictCommonModel> dictCommon) {
        this.inputImportCostDao = inputImportCostDao;
        this.callBack = callBack;
        this.dictCommon = dictCommon;
        this.extProPlan = extProPlan;
    }


    @Override
    public void invoke(InputExtProPlanModel data, AnalysisContext context) {

//        DictCommonModel dictCommon = getName(data.getOutAssistType());
        data.setPartName(extProPlan.getInputPartName());
        data.setKind(extProPlan.getInputKind());

        if (data.getOutMalDesc() == null || data.getOutMalDesc().trim().equals("")) {
            data.setReason(data.getReason() + "物料描述没填");
        }
        if (data.getOutDiagram() == null || data.getOutDiagram().trim().equals("")) {
            data.setReason(data.getReason() + "图号没填");
        }
        if (data.getOutNum() == null) {
            data.setReason(data.getReason() + "数量没填");
        }
        else if (data.getOutNum() <= 0) {
            data.setReason(data.getReason() + "数量必须大于0");
        }
        if (data.getOutUnit() == null || data.getOutUnit().trim().equals("")) {
            data.setReason(data.getReason() + "单位没填");
        }

        if (data.getOutAssistType() == null || data.getOutAssistType().trim().equals("")) {
            data.setReason(data.getReason() + "外协类型没填");
        } else if (getName(data.getOutAssistType()) == null){
            data.setReason(data.getReason() + "外协类型不存在");
        }

        if (inputImportCostDao.selectExtProPlanRepetition(data) > 0) {
            data.setReason(data.getReason() + "重复数据");
        }
         /**判断并输出验证结果*/
        if (data.getReason()!= null && data.getReason().length() > 0) {
            inputExtProPlanModel.add(data);
            i++;
            return;
        }
        User user = ShiroUtils.getUser();
        data.setInputUser(user.getName());
        data.setEditDate(new Date());
        data.setFkId(extProPlan.getKbId());

        data.setDesignType(extProPlan.getInputDesignType());
        data.setFkInputId(extProPlan.getInputMacId());

        data.setCostKind(extProPlan.getInputCostKind());
        data.setFkCode(extProPlan.getInputCode());
        data.setIsReceive(0);
        data.setOutDeleteFlag(0);
        data.setOutReportType(2);
        inputImportCostDao.insertExtProPlan(data);
        i++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (exceptionList.size() > 0) {
            InputExtProPlanModel model = new InputExtProPlanModel();
            model.setOutMalDesc("第" + exceptionList.toString() + "行数据格式不正确");
            inputExtProPlanModel.add(model);
        }
        if (inputExtProPlanModel.size() > 0) {
            callBack.onWarning(inputExtProPlanModel);
        } else {
            callBack.onSuccess();
        }
    }
    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exception.printStackTrace();
        String err = exception.getMessage();
        exceptionList.add(i);
        i++;
        return;
    }

    private DictCommonModel getName(String dictCommonTypes) {
        for (DictCommonModel dictCommon : dictCommon) {
            if (dictCommon.getName() != null && dictCommon.getName().equals(dictCommonTypes)) {
                return dictCommon;
            }
        }
        return null;
    }

}
